package chessBoard;

import org.apache.log4j.Logger;

import connection.Reader;

/**
 * Trida pro ovladani casu na info panelu
 * 
 * @author dove
 * 
 */
public class Time extends Thread {

	private InfoPanel time;

	private boolean running = false;

	private static Logger logger = Logger.getLogger("Log");

	/**
	 * Aktualni cas hrace
	 * 
	 * @param time
	 *            cas
	 */
	public Time(InfoPanel time) {
		this.time = time;
	}

	public void run() {
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				logger.error("", e);
			}
			if (!Reader.isConnect()) {
				running = false;
			}
			if (running) {
				if (time.isFirstActive()) {
					time.decreaseTimeOne();
				} else if (time.isSecondActive()) {
					time.decreaseTimeTwo();
				}
			}
		}
	}

	/**
	 * Zjistuje jestli hodiny bezi
	 * 
	 * @return
	 */
	public boolean isRunning() {
		return running;
	}

	/**
	 * Spousti chod hodin
	 */
	public void startClock() {
		running = true;
	}

	/**
	 * Zastavuje hodiny
	 */
	public void stopClock() {
		running = false;
	}

	/**
	 * Nastavuje chod hodin
	 * 
	 * @param running
	 *            true jestlize maji bezet false jestlize nemaji bezet
	 */
	public void setRunning(boolean running) {
		logger.debug("TIME:" + running);
		this.running = running;
	}
}
